package week9.TEST;//*******************************************************************
//  MagazineList.java       Author: Lewis/Loftus
//
//  Represents a collection of magazines.
//*******************************************************************

import week9.TEST.Magazine;

import java.util.ArrayList;

public class MagazineList
{
   private MagazineNode list;
int a=1;
   //----------------------------------------------------------------
   //  Sets up an initially empty list of magazines.
   //----------------------------------------------------------------
   public MagazineList()
   {
      list = null;
   }

   //----------------------------------------------------------------
   //  Creates a new MagazineNode object and adds it to the end of
   //  the linked list.
   //----------------------------------------------------------------
   ArrayList<MagazineNode> y =  new ArrayList<MagazineNode>();
    public void add(Magazine mag)
   {
      MagazineNode node = new MagazineNode(mag);
      MagazineNode current;

      if (list == null)
         list = node;
      else
      {
         current = list;
         while (current.next != null)
            current = current.next;
         current.next = node;

      }
   }

   public void insert(int index, Magazine newMagazine){
       MagazineNode node1=new MagazineNode(newMagazine);
       MagazineNode current2=node1;
       MagazineNode current1 =list;
       MagazineNode current3=list;
       MagazineNode current4=list;
       MagazineNode current6=list;
       while (current1.next!=null)
       {a++;current1=current1.next;}
       if (index==1)
       {node1.next=current4;
       list=node1;}
     if (1<index&index<a+1) {
         for (int a = 2; a < index; a++)
         {
             if (current4.next!=null)
                 current4= current4.next;
         }
         for(int a=2;a<index+1;a++){
             if(current6.next!=null)
                 current6= current6.next; }
         MagazineNode current5=current4;
         current5.next=node1;
         node1.next=current6; }
       if(index==a+1) {
               for (int an = 0; an < a; an++)
               {  if(current3.next!=null)
                    current3=current3.next; }
           current3.next=node1; }
           }

    public void delete(Magazine delNode) {
        MagazineNode pre=list.next;
        MagazineNode current=list;
        MagazineNode current1=pre;
        MagazineNode current2=list;
        MagazineNode current3=pre;
        MagazineNode current4=list;
        while(current4.magazine.toString()!=delNode.toString()&&current4.next!=null)
            current4=current4.next;
        if(current4.next==null)
            System.out.println("进行的删除有误请重新进行");
        else
        {if(delNode.toString()==(list.magazine.toString()))
            list=list.next;
        else {
            while (current1.next != null) {
                current1 = current1.next;

                current = current.next;
            }
            if (delNode.toString() == current1.magazine.toString())
                current.next = null;
            else
            {
                while(current3.magazine.toString()!=delNode.toString()&&current3!=null)
                {current3=current3.next;
                    current2=current2.next;}
                    current2.next=current3.next;
            } }}
        }

   //----------------------------------------------------------------
   //  Returns this list of magazines as a string.
   //----------------------------------------------------------------
   public String toString()
   {
      String result = "";

      MagazineNode current = list;

      while (current != null)
      {
         result += current.magazine + "\n";
         current = current.next;
      }

      return result;
   }

   //*****************************************************************
   //  An inner class that represents a node in the magazine list.
   //  The public variables are accessed by the MagazineList class.
   //*****************************************************************
   private class MagazineNode
   {
      public Magazine magazine;
      public MagazineNode next;

      //--------------------------------------------------------------
      //  Sets up the node
      //--------------------------------------------------------------
      public MagazineNode(Magazine mag)
      {
         magazine = mag;
         next = null;
      }
   }
}
